<template>
  <div class="cart">
    <nav-bar class="nav-bar">
      <div slot="center">购物车({{ cartLength }})</div>
    </nav-bar>
    <!-- 商品信息 -->
    <div class="cart-content" v-for="(item, index) in cartList" :key="index">
      <input
        type="checkbox"
        :checked="item.checked"
        @click="checkClick(index)"
      />
      <img :src="item.image" alt="" />
      <span class="cart-title">{{ item.title }}</span>
      <span class="cart-desc">{{ item.desc }}</span>
      <span class="cart-price">￥{{ item.price }}</span>
      <span class="cart-count">×{{ item.count }}</span>
    </div>
    <!-- 结算信息 -->
    <div class="cart-bottom">
      <div class="cart-bottom-left">
        <input type="checkbox" :checked="checkAll" @click="checkAllClick" />
        <span class="check-all">全选</span>
        <span>合计：{{ totalPrice }}</span>
      </div>
      <div class="cart-bottom-right">
        <span>去计算({{ checkLength }})</span>
      </div>
    </div>
  </div>
</template>

<script>
import NavBar from "@/components/common/navbar/NavBar";
import Scroll from "@/components/common/scroll/Scroll";

export default {
  components: {
    NavBar,
    Scroll,
  },
  data() {
    return {
      cartList: [],
    };
  },
  computed: {
    cartLength() {
      return this.cartList.length;
    },
    // 全选
    checkAll() {
      let checkLength = 0;
      for (let cart of this.cartList) {
        if (cart.checked) {
          checkLength++;
        }
      }
      if (checkLength == this.cartList.length) {
        return true;
      } else {
        return false;
      }
    },
    // 统计总价
    totalPrice() {
      let totalPrice = 0;
      for (let cart of this.cartList) {
        if (cart.checked) {
          let price = cart.count * cart.price;
          totalPrice += price;
        }
      }
      return "￥" + totalPrice.toFixed(2);
    },
    // 计算总数
    checkLength() {
      let length = 0;
      for (let cart of this.cartList) {
        if (cart.checked) {
          length++;
        }
      }
      return length;
    },
  },
  mounted() {
    this.cartList = this.$store.state.cartList;
  },
  methods: {
    // 选择按钮
    checkClick(index) {
      this.cartList[index].checked = !this.cartList[index].checked;
    },
    // 全选按钮的点击效果
    checkAllClick() {
      if(this.checkAll) {
        this.cartList.forEach(item=>{ item.checked = false})
      } else {
        this.cartList.forEach(item=>{ item.checked = true})
      }
    },
  },
};
</script>

<style scoped>
.nav-bar {
  background-color: var(--color-tint);
  color: #fff;
  font-weight: 600;
  font-size: 18px;
}
.cart-content {
  position: relative;
  height: 100px;
  width: 100%;
  padding-right: 30px;
  border-bottom: 1px solid #aaa;
}
.cart-content input {
  position: absolute;
  top: 45px;
  left: 3px;
}
.cart-content img {
  width: 60px;
  height: 80px;
  margin-left: 20px;
  margin-top: 10px;
  border-radius: 5px;
}
.cart-title {
  position: absolute;
  top: 10px;
  font-size: 15px;
  width: 75%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-left: 10px;
}
.cart-desc {
  position: absolute;
  width: 75%;
  top: 40px;
  font-size: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-left: 10px;
}
.cart-price {
  position: absolute;
  top: 70px;
  margin-left: 10px;
  color: orange;
}
.cart-count {
  position: absolute;
  top: 70px;
  right: 20px;
}
.cart-bottom {
  position: fixed;
  bottom: 49px;
  height: 30px;
  display: flex;
  left: 0;
  right: 0;
  background-color: #eee;
  line-height: 30px;
  padding-left: 10px;
}
.cart-bottom input {
  margin-right: 3px;
}
.cart-bottom .cart-bottom-left {
  flex: 7;
}
.cart-bottom-left span {
  font-size: 14px;
}
.cart-bottom-left .check-all {
  font-size: 12px;
  margin-right: 20px;
}
.cart-bottom .cart-bottom-right {
  background-color: orange;
  flex: 3;
}
.cart-bottom-right {
  font-size: 13px;
  line-height: 30px;
  text-align: center;
}
</style>
